﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace InformationSystem.Utility
{
    public class MSecurity
    {
        private const string EncryptKey = "A_db9Fu8*OSasBe3";
        private const string publicKey = "<RSAKeyValue><Modulus>xgCXksvwExkBpTxKB+t7JvhIXP1i0FZh8hTvLomtyV17telr3voGZItJspmbKdyN898VPIfh2OTzO5ew78ymDQBVcH59PdxxdaCqESpmqMVboJju9pIQywJtNc1H9oNCaMbqz4o0OQebadt8j6B6oFr39uhddUwlMkFezHNkQcrlD93VAGGHf5cw2bTGmfLqXGddrXlXMJejRFh23bEB6l8dfNBCtKub+IhYCm/5iCdeMt2obsiSrg/jraVTwlnCpDlZ6CI42KuRz/drOZx57ItzHdaGwnugLKhfqQiuvits5tiOnSgKwLy3gFMiLurbPE1eRWJcpcYV0PBldFUqyQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        private static readonly byte[] IV =
            new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

        /// <summary>
        /// TripleDES Encrypt
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);

                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(EncryptKey);
                des.IV = IV;
                result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

        /// <summary>
        ///TripleDES Decrypts provided string parameter
        /// </summary>
        public static string Decrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Convert.FromBase64String(s);

                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

                des.Key = ASCIIEncoding.ASCII.GetBytes(EncryptKey);

                des.IV = IV;

                result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                return string.Empty;
            }

            return result;
        }

        public static string Decrypt(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {

                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

                des.Key = ASCIIEncoding.ASCII.GetBytes(EncryptKey);

                des.IV = IV;

                result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                return string.Empty;
            }

            return result;
        }
        /// <summary>
        /// MD5 Encryption with no Decrypt
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Encryption(string password)
        {
            byte[] value;
            string pwd = string.Empty;
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            UTF8Encoding encoding = new UTF8Encoding();
            value = md5Hasher.ComputeHash(encoding.GetBytes(password));
            for (int i = 0; i < value.Length; i++)
            {
                pwd = pwd + value[i].ToString("X").PadLeft(2, '0');
            }
            return pwd;
        }

        public static bool RSAVerify(byte[] data, byte[] sign)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicKey);
            try
            {
                return rsa.VerifyData(data, "SHA1", sign);
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static byte[] RSASign(byte[] data, string privatekey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(privatekey);
            try
            {
                return rsa.SignData(data, "SHA1");
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
